{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "380333a8",
   "metadata": {},
   "source": [
    "* 2023-10-29\n",
    "# 3. 变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "373aa72b",
   "metadata": {},
   "source": [
    "> a.变量随用随写，可以来回赋值不同类型的值，把变量理解成一个标签纸，赋值给谁就贴在谁的头上。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7dc2b298",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'>\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "print(type(a))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a5b18532",
   "metadata": {},
   "source": [
    "> b.变量可以大小写、下划线开头，不要以数字，特殊符号开头，建议日常以小写字母开头且能见名知意就好。  \n",
    "> c.变量区分大小写，比如AA、Aa、aA是不同的变量。  \n",
    "> d.python的保留字不能用作变量，比如def、while、for...。是保留字会变颜色的！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7e5e4b9",
   "metadata": {},
   "source": [
    "# 4.字符串\n",
    "* 单引号 ''\n",
    "* 双引号 \"\"\n",
    "* 三引号 \"\"\" # 如果字符长度长，要跨行那种，一般都用三引号."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d3e9cb9",
   "metadata": {},
   "source": [
    "## 4.1字符串的拼接\n",
    "* 字符串拼接用+号，主要类型是str（字符串类型）结果也是字符串类型  \n",
    "print(str1+ str2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53a1b457",
   "metadata": {},
   "source": [
    "* 如果遇到一些非字符串类型的变量需要拼接，如数值int类型，可以先将其转换成字符串，然后再按上面的套路，用+号进行拼接处理。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "abe69698",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网段192.168.0.0/24有60\n"
     ]
    }
   ],
   "source": [
    "statement1 = '网段192.168.0.0/24有'\n",
    "quantity = 60\n",
    "all_str = statement1 + str(quantity)\n",
    "print(all_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e67d842",
   "metadata": {},
   "source": [
    "## 4.2字符串的方法\n",
    "* 常见的字符串有：count()、input()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "982d93d8",
   "metadata": {},
   "source": [
    "> 1.count()方法返回字符串里面含有多少个()里面的参数，注意这个方法一定要带参数(argument)，否则会报错。\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d5a1f802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "ipaddress = \"    192.15.1.1    \"\n",
    "print(ipaddress.count('1'))\n",
    "# 数1这个数字在里面有多少次重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ef4edaf9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入ip地址 \n",
      "1.1.1.1\n",
      "1.1.1.1\n"
     ]
    }
   ],
   "source": [
    "ipaddress = input('请输入ip地址 \\n')\n",
    "print(ipaddress)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71aa34de",
   "metadata": {},
   "source": [
    "# 5.运算符与字符串互动"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10ada4a0",
   "metadata": {},
   "source": [
    "## 5.1 字符串与*联动\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f3a3ff87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "huaweihuaweihuaweihuaweihuawei\n"
     ]
    }
   ],
   "source": [
    "a = \"huawei\"\n",
    "print(a*5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52b035d0",
   "metadata": {},
   "source": [
    "## 5.2分割线妙用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c7709018",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************************\n"
     ]
    }
   ],
   "source": [
    "a = '********' * 3\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a65b25ac",
   "metadata": {},
   "source": [
    "## 5.3 字符串与+联动"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ec5257ad",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid character '（' (U+FF08) (2854718153.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[16], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    print（'asd '+'asd ')\u001b[0m\n\u001b[1;37m         ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid character '（' (U+FF08)\n"
     ]
    }
   ],
   "source": [
    "print（'asd '+'asd ')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "996f32d3",
   "metadata": {},
   "source": [
    "## 5.4统计，累加+="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c1df5f5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "b = 1\n",
    "b +=1\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97682aac",
   "metadata": {},
   "source": [
    "# 6.列表概念与元素\n",
    "* 有序的集合，用中括号[]括起来，从[]空列表开始，你想装啥就装啥"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7439606c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "list = [1,'huawei',[1,2],5]\n",
    "print(list[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "606a834b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 5]\n"
     ]
    }
   ],
   "source": [
    "print(list[::3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2031ff7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n"
     ]
    }
   ],
   "source": [
    "print(len(list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8e8f76f",
   "metadata": {},
   "source": [
    "# 7.列表方法与函\n",
    "* range()返回的是一个range类型对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b2d2db2a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'range'>\n"
     ]
    }
   ],
   "source": [
    "a = range(5)\n",
    "print(type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "66354ea8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "12\n",
      "14\n",
      "16\n",
      "18\n"
     ]
    }
   ],
   "source": [
    "for i in range(0,20,2):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41c392b3",
   "metadata": {},
   "source": [
    "* append() 这个方法在列表末位添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5573eab0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入N的值：\n",
      "10\n",
      "10 年后，兔子的数量为6.\n",
      "10 年后，兔子的数量为12.\n",
      "10 年后，兔子的数量为24.\n",
      "10 年后，兔子的数量为48.\n",
      "10 年后，兔子的数量为96.\n",
      "10 年后，兔子的数量为192.\n",
      "10 年后，兔子的数量为384.\n",
      "10 年后，兔子的数量为768.\n",
      "10 年后，兔子的数量为1536.\n",
      "10 年后，兔子的数量为3072.\n"
     ]
    }
   ],
   "source": [
    "rabbit = 3\n",
    "print(\"请输入N的值：\")\n",
    "N = int(input())\n",
    "for i in range(0,N):\n",
    "    rabbit = rabbit*2\n",
    "    print(\"%d 年后，兔子的数量为%d.\"%(N,rabbit))\n",
    "    # print(\"i = %d\"%(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "66704bf0",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3605463871.py, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[13], line 2\u001b[1;36m\u001b[0m\n\u001b[1;33m    for i range(0,N):\u001b[0m\n\u001b[1;37m          ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "dog = 0\n",
    "for i range(0,N):\n",
    "    N = int(input())\n",
    "    if N=0:\n",
    "        print(\"0\")\n",
    "    else:\n",
    "        dog = dog+1\n",
    "        print(\"%d年后，狗的数量为%d只\"%(N,dog))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "e82797d3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gi0/0/1\n",
      "gi0/0/2\n",
      "gi0/0/3\n",
      "gi0/0/4\n"
     ]
    }
   ],
   "source": [
    "interfaces = []\n",
    "interfaces.append('gi0/0/1')\n",
    "interfaces.append('gi0/0/2')\n",
    "interfaces.append('gi0/0/3')\n",
    "interfaces.append('gi0/0/4')\n",
    "for i in interfaces:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbd9a983",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "efaa99b8",
   "metadata": {},
   "source": [
    "* insert() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "477016af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# append()只能用在列表末位添加元素的场景，远远不能满足我们日常的生产应用,于是它出来露露脸"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "45240470",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gi0/0/1\n",
      "gi0/0/2\n",
      "a\n",
      "gi0/0/3\n",
      "gi0/0/4\n"
     ]
    }
   ],
   "source": [
    "interfaces = []\n",
    "interfaces.append('gi0/0/1')\n",
    "interfaces.append('gi0/0/2')\n",
    "interfaces.append('gi0/0/3')\n",
    "interfaces.append('gi0/0/4')\n",
    "interfaces.insert(2,'a')\n",
    "for i in interfaces:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcd08b55",
   "metadata": {},
   "source": [
    "* pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d77073de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 移除，移除操作后那个元素作为返回值的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e1c04f0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gi0/0/1\n",
      "gi0/0/3\n",
      "a\n",
      "gi0/0/4\n"
     ]
    }
   ],
   "source": [
    "interfaces = []\n",
    "interfaces.append('gi0/0/1')\n",
    "interfaces.append('gi0/0/2')\n",
    "interfaces.pop()\n",
    "interfaces.append('gi0/0/3')\n",
    "interfaces.append('gi0/0/4')\n",
    "interfaces.insert(2,'a')\n",
    "\n",
    "for i in interfaces:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "9822ebd0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop放哪，就删上面那一条"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21e494e1",
   "metadata": {},
   "source": [
    "# 8 字典概念与元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86665ed0",
   "metadata": {},
   "source": [
    "## 8.1 python中，字典是有序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4443d30",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
